#Boa:Frame:MainFrame

import wx
import GUIService
import SettingDialog
from wx.lib.newevent import NewEvent
LogEvent,EVT_LOG = NewEvent()
StatusEvent,EVT_STA = NewEvent()

def create(parent):
    return MainFrame(parent)

[wxID_MAINFRAME, wxID_MAINFRAMEEXPANDBUTTON, wxID_MAINFRAMEFETCHSERVERTEXT, 
 wxID_MAINFRAMEFETCHSERVERVALUETEXT, wxID_MAINFRAMEIPV4STATUSTEXT, 
 wxID_MAINFRAMEIPV4STATUSVALUETEXT, wxID_MAINFRAMEIPV6STATUSTEXT, 
 wxID_MAINFRAMEIPV6STATUSVALUETEXT, wxID_MAINFRAMELISTENADDRESSTEXT, 
 wxID_MAINFRAMELISTENADDRESSVALUE, wxID_MAINFRAMEPANEL1, 
 wxID_MAINFRAMESETTINGBUTTON, wxID_MAINFRAMESTATUSTEXT, 
 wxID_MAINFRAMESTATUSVALUETEXT, wxID_MAINFRAMETEXTCTRL1, 
] = [wx.NewId() for _init_ctrls in range(15)]

class MainFrame(wx.Frame):
    StatusValueDict = None
        
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.Ipv4StatusText, 0, border=0, flag=0)
        parent.AddWindow(self.Ipv4StatusValueText, 0, border=0, flag=0)
        parent.AddWindow(self.Ipv6StatusText, 0, border=0, flag=0)
        parent.AddWindow(self.Ipv6StatusValueText, 0, border=0, flag=0)

    def _init_coll_flexGridSizer2_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.StatusText, 0, border=0, flag=0)
        parent.AddWindow(self.StatusValueText, 0, border=0, flag=0)
        parent.AddWindow(self.FetchServerText, 0, border=0, flag=0)
        parent.AddWindow(self.FetchServerValueText, 0, border=0, flag=0)
        parent.AddWindow(self.ListenAddressText, 0, border=0, flag=0)
        parent.AddWindow(self.ListenAddressValue, 0, border=0, flag=0)

    def _init_coll_boxSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddSizer(self.flexGridSizer2, 0, border=5,
              flag=wx.TOP | wx.RIGHT | wx.LEFT)
        parent.AddSizer(self.flexGridSizer1, 0, border=5,
              flag=wx.RIGHT | wx.LEFT)
        parent.AddSizer(self.boxSizer2, 0, border=5,
              flag=wx.LEFT | wx.RIGHT | wx.TOP)
        parent.AddWindow(self.textCtrl1, 1, border=5, flag=wx.ALL | wx.EXPAND)

    def _init_coll_boxSizer2_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.ExpandButton, 0, border=5, flag=wx.RIGHT)
        parent.AddWindow(self.SettingButton, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)

        self.flexGridSizer1 = wx.FlexGridSizer(cols=4, hgap=0, rows=0, vgap=0)

        self.boxSizer2 = wx.BoxSizer(orient=wx.HORIZONTAL)

        self.flexGridSizer2 = wx.FlexGridSizer(cols=2, hgap=0, rows=0, vgap=0)

        self._init_coll_boxSizer1_Items(self.boxSizer1)
        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_boxSizer2_Items(self.boxSizer2)
        self._init_coll_flexGridSizer2_Items(self.flexGridSizer2)

        self.panel1.SetSizer(self.boxSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_MAINFRAME, name='MainFrame',
              parent=prnt, pos=wx.Point(608, 436), size=wx.Size(398, 253),
              style=wx.DEFAULT_FRAME_STYLE, title='Dreamer')
        self.SetClientSize(wx.Size(382, 217))
        self.Bind(wx.EVT_CLOSE, self.OnMainFrameClose)

        self.panel1 = wx.Panel(id=wxID_MAINFRAMEPANEL1, name='panel1',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(382, 217),
              style=wx.TAB_TRAVERSAL)

        self.Ipv4StatusText = wx.StaticText(id=wxID_MAINFRAMEIPV4STATUSTEXT,
              label='Ipv4 Status', name='Ipv4StatusText', parent=self.panel1,
              pos=wx.Point(5, 47), size=wx.Size(63, 14), style=0)

        self.Ipv4StatusValueText = wx.StaticText(id=wxID_MAINFRAMEIPV4STATUSVALUETEXT,
              label='               ', name='Ipv4StatusValueText',
              parent=self.panel1, pos=wx.Point(68, 47), size=wx.Size(60, 14),
              style=0)

        self.Ipv6StatusText = wx.StaticText(id=wxID_MAINFRAMEIPV6STATUSTEXT,
              label='Ipv6 Status', name='Ipv6StatusText', parent=self.panel1,
              pos=wx.Point(128, 47), size=wx.Size(72, 14), style=0)

        self.Ipv6StatusValueText = wx.StaticText(id=wxID_MAINFRAMEIPV6STATUSVALUETEXT,
              label='                         ', name='Ipv6StatusValueText',
              parent=self.panel1, pos=wx.Point(200, 47), size=wx.Size(-1, -1),
              style=0)

        self.ExpandButton = wx.Button(id=wxID_MAINFRAMEEXPANDBUTTON,
              label='Expand', name='ExpandButton', parent=self.panel1,
              pos=wx.Point(5, 66), size=wx.Size(75, 24), style=0)
        self.ExpandButton.Bind(wx.EVT_BUTTON, self.OnExpandButtonButton,
              id=wxID_MAINFRAMEEXPANDBUTTON)

        self.SettingButton = wx.Button(id=wxID_MAINFRAMESETTINGBUTTON,
              label='Setting', name='SettingButton', parent=self.panel1,
              pos=wx.Point(85, 66), size=wx.Size(75, 24), style=0)
        self.SettingButton.Bind(wx.EVT_BUTTON, self.OnSettingButtonButton,
              id=wxID_MAINFRAMESETTINGBUTTON)

        self.textCtrl1 = wx.TextCtrl(id=wxID_MAINFRAMETEXTCTRL1,
              name='textCtrl1', parent=self.panel1, pos=wx.Point(5, 95),
              size=wx.Size(372, 117),
              style=wx.VSCROLL | wx.TE_READONLY | wx.TE_MULTILINE, value='')

        self.StatusText = wx.StaticText(id=wxID_MAINFRAMESTATUSTEXT,
              label='Status:', name='StatusText', parent=self.panel1,
              pos=wx.Point(5, 5), size=wx.Size(62, 14), style=0)

        self.StatusValueText = wx.StaticText(id=wxID_MAINFRAMESTATUSVALUETEXT,
              label='Please Wait', name='StatusValueText', parent=self.panel1,
              pos=wx.Point(88, 5), size=wx.Size(69, 14), style=0)

        self.FetchServerText = wx.StaticText(id=wxID_MAINFRAMEFETCHSERVERTEXT,
              label='Fetch Server:', name='FetchServerText', parent=self.panel1,
              pos=wx.Point(5, 19), size=wx.Size(70, 14), style=0)

        self.FetchServerValueText = wx.StaticText(id=wxID_MAINFRAMEFETCHSERVERVALUETEXT,
              label='', name='FetchServerValueText', parent=self.panel1,
              pos=wx.Point(88, 19), size=wx.Size(-1, -1), style=0)

        self.ListenAddressText = wx.StaticText(id=wxID_MAINFRAMELISTENADDRESSTEXT,
              label='Listen Address:', name='ListenAddressText',
              parent=self.panel1, pos=wx.Point(5, 33), size=wx.Size(83, 14),
              style=0)

        self.ListenAddressValue = wx.StaticText(id=wxID_MAINFRAMELISTENADDRESSVALUE,
              label='', name='ListenAddressValue', parent=self.panel1,
              pos=wx.Point(88, 33), size=wx.Size(-1, -1), style=0)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.Bind(EVT_LOG, self.OnLog)
        self.Bind(EVT_STA, self.OnStatusChange)
        self.logCount = 0
        self.StatusValueDict = {
            "Status":self.StatusValueText,
            "Listen Addr": self.ListenAddressValue,
        "Fetch Server": self.FetchServerValueText,
        "Ipv4 State": self.Ipv4StatusValueText,
        "Ipv6 State": self.Ipv6StatusValueText}

    def OnMainFrameClose(self, event):
        self.presenter.shutdown()
        event.Skip()

    def OnExpandButtonButton(self, event):
        event.Skip()

    def OnSettingButtonButton(self, event):
        dlg = SettingDialog.create(self)
        dlg.ShowModal()

    def writeLog(self,string):
        wx.PostEvent(self, LogEvent(data=string))

    def proxyInit(self,status):
        wx.PostEvent(self, StatusEvent(data= status))
        
    def OnLog(self,event):
        self.logCount += 1
        if self.logCount > 50:
            self.logCount = 0
            self.textCtrl1.Clear()
        self.textCtrl1.AppendText(event.data)

    def OnStatusChange(self,event):
        statusDict = event.data
        for key in self.StatusValueDict:
            self.StatusValueDict[key].SetLabel(statusDict[key])

class Proxy(wx.App):
    def OnInit(self):
        self.frame = create(None)
        self.frame.Show()
        presenter = GUIService.Presenter(self.frame.writeLog,self.frame.proxyInit)
        self.frame.presenter = presenter
        GUIService.startProxyThread(presenter)
        return True

def main():
    app = Proxy(0)
    app.MainLoop()

if __name__ == '__main__':
    main()
